﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using ConnectDatabase;

namespace Web
{
    /// <summary>
    /// Summary description for SearchCity
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    [System.Web.Script.Services.ScriptService]
    public class SearchCity : System.Web.Services.WebService
    {

        [WebMethod]
        public string[] GetCompletionList(string prefixText)
        {
            //List<tblGeoAirport> data = new GeoAirportDB().SearchByKeyword(prefixText);
            List<ConnectDatabase.GeoAirportDB.RawData> data = new GeoAirportDB().SearchByKeyword(prefixText);
            List<string> list = new List<string>();
            foreach (ConnectDatabase.GeoAirportDB.RawData obj in data)
            {
                string text = "";
                if (obj.CityCode == obj.AirportCode)
                {
                    //text = obj.AirportName + " - " + obj.CityName + ", " + obj.CountryName + " (" + obj.CityCode + ")";
                    text = obj.CityName + ", " + obj.CountryName + " (" + obj.CityCode + ")";
                }
                else
                {
                    text = "→ " + obj.AirportName + " (" + obj.AirportCode + ")" + " - " + obj.CityName + ", " + obj.CountryName;
                }

                list.Add(AjaxControlToolkit.AutoCompleteExtender.CreateAutoCompleteItem(text, obj.AirportCode));
            }

            list = list.OrderByDescending(a => a).ToList();

            return list.ToArray();
        }
    }
}
